import Vue from "vue";
import VueRouter from 'vue-router'
import Home from '../pages/Home'
import Login from '@/pages/Login'
import store from "@/store";

Vue.use(VueRouter)
// 重写push
let originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, resolve, reject) {
        if (resolve && reject) {
                return originPush.call(this, location).then(resolve, reject)
        } else {
                return originPush.call(this, location).then(() => { }, (error) => error)
        }
}
// 重写replace
let originReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function (location, resolve, reject) {
        if (resolve && reject) {
                return originReplace.call(this, location).then(resolve, reject)
        } else {
                return originReplace.call(this, location).then(() => { }, (error) => { return error })
        }
}
const routes = [
        {
                path: '/center', component: () => import('@/pages/Center'), redirect: '/center/myorder',
                children: [
                        { path: 'myorder', component: () => import('@/pages/Center/MyOrder') },
                        { path: 'grouporder', component: import('@/pages/Center/GroupOrder') },
                ],
                meta: { showFooter: true ,title:'个人中心'}
        },
        {
                path: '/home', component: Home,
                meta: { showFooter: true }
        },
        {
                path: '/login', component: Login,name:'login',
                meta: { showFooter: false ,title:'登录'}
        },
        {
                path: '/register', component: () => import('@/pages/Register'),name:'register',
                meta: { showFooter: false ,title:'注册'}
        },
        {
                path: '/search/:keyword?', component: () => import('@/pages/Search'), name: 'search',
                meta: { showFooter: true ,title:'搜索'}
        },
        {
                path: '/detail/:skuId', component: () => import('@/pages/Detail'), name: 'detail',
                meta: { showFooter: true ,title:'商品详情'}
        },
        {
                path: '/addcartsucess', component: () => import('@/pages/AddCartSuccess'), name: 'addcartsucess',
                meta: { showFooter: true ,title:'添加成功'}
        },
        {
                path: '/shopcart', component: () => import('@/pages/ShopCart'), name: 'shopcart',
                meta: { showFooter: true ,title:'购物车'}
        },
        {
                path: '/trade', component: () => import('@/pages/Trade'), name: 'trade',
                meta: { showFooter: true ,title:'订单'}
        },
        {
                path: '/pay', component: () => import('@/pages/Pay'), name: 'pay',
                meta: { showFooter: true ,title:'支付'},
                beforeEnter: (to, from, next) => {
                        if (from.path === '/trade') {
                                next()
                        }
                }
        },
        {
                path: '/paysuccess', component: () => import('@/pages/PaySuccess'), name: 'paysuccess',
                meta: { showFooter: true ,title:'支付成功'}
        },
        { path: '/', redirect: '/home' }
]

const router = new VueRouter({
        routes,
        scrollBehavior(to, from, savedPosition) {
                return { y: 0 }
        }
})
router.beforeEach(async (to, from, next) => {
        if(to.meta.title)document.title=to.meta.title+'-尚品汇'
        const token = store.state.user.token
        const name = store.state.user.userInfo.name
        if (token) {
                // 已经登陆
                if (to.path === '/login') {
                        next('/home')
                } else {
                        if (name) {
                                // 已有用户信息
                                next()
                        } else {
                                try {
                                        await store.dispatch('getUserInfo')
                                        next()
                                } catch (error) {
                                        await store.dispatch('logout')
                                        next('/login')
                                }
                        }
                }
        } else {
                // 未登录,如果路径含有以下名字跳转至登录页面
                let pathArray = ['pay', 'trade', 'center']
                let hasPath = () => {
                        return !pathArray.every(i => to.path.indexOf(i) === -1)
                }
                if (hasPath()) {
                        next('/login?redirect=' + to.path)
                }
                next()
        }
})
export default router